router.get("/edit",authenLogin, function (req, res, next) {
    const id = req.query.id;
    var sql ='select * from product where id=?';
    var sqlParam = [id];
    db.conn.query(sql, sqlParam, function (err, result) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }
        res.render("product/productedit", { item: result[0] });
    });
});
router.post("/doEdit",authenLogin, function (req, res, next) {
    const form = new multiparty.Form();
    form.uploadDir = "public/upload";
    form.parse(req, function (err, fields, files) {
        const id = fields._id[0];
        console.log(id);
        const title = fields.title[0];
        const cat_id = fields.cat_id[0];
        const price = fields.price[0];
        const description = fields.description[0];
        const keywords = fields.keywords[0];
        const originalFilename = files.pic[0].originalFilename; // 取原始文件名
        console.log(originalFilename);
        // 去掉public/，默认情况下会自动到public文件夹中找静态资源文件夹
        const pic = files.pic[0].path.substr(7);
        console.log(pic);
        // originalFilename有值，表示修改了图片
        // originalFilename无值，表示未修改图片
        console.log(originalFilename.length); // 上传图片时，长度大于0，否则为0
        if (originalFilename.length > 0) {
            // 有修改图片，图片信息为: upload\b2mBGtaeFwnrAmNHwj4DaF.jpg
            var sql = 'update product set name=?,cat_id=?, price=?,content=?, image=?,keywords=? where id=?';
            var sqlParam = [title, cat_id, price, description, pic, keywords, id];
        } else {
            // 未修改图片
            var sql = 'update product set name=?,cat_id=?,price=?,content=?, keywords=? where id=?';
            var sqlParam = [title, cat_id, price, description, keywords, id];
        }
        console.log(sqlParam);
        db.conn.query(sql, sqlParam, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            }
            res.redirect("/admin/product");
        });
    });
});